package com.wowudi.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wowudi.dto.ListDto;
import com.wowudi.entity.HoPay;
import com.wowudi.entity.OperationRecord;
import com.wowudi.entity.OperationType;
import com.wowudi.entity.Patients;
import com.wowudi.service.OperationRecordService;
import com.wowudi.service.OperationTypeService;
import com.wowudi.service.PatientsService;
import com.wowudi.util.MsgUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Random;

@RestController
@RequestMapping("api/fina/record")
public class RecordController {
    @Autowired
    private OperationRecordService operationRecordService;
    @Autowired
    private OperationTypeService operationTypeService;
    @Autowired
    private PatientsService patientsService;
    @RequestMapping("findRecord")
    public Page<OperationRecord> findPati(Integer pageym , Integer pagesize, String cartid, String patiname) {
        Page<OperationRecord> page = new Page<OperationRecord>(pageym,pagesize);
      //  this.operationRecordService.page(page);
         this.operationRecordService.getAllBynameandid(page,cartid,patiname);
        return page;
    }
    //删除
    @RequestMapping("delRecord")
    public MsgUtil delPati(Integer id) {
        OperationRecord operationRecord=this.operationRecordService.getById(id);
        operationRecord.setEqIsdel(1);
        boolean bool=this.operationRecordService.updateById(operationRecord);
        if (bool){
            return new MsgUtil(true,"删除成功",null);
        }else {
            return new MsgUtil(false, "删除失败", null);
        }
    }
    @RequestMapping("getListDto")
    public ListDto getListDto() {
        ListDto listDto=new ListDto();
        listDto.setPackageList(this.patientsService.list());
        QueryWrapper<OperationType> wrapper=new QueryWrapper<>();
        wrapper.in("hoy_status","y");
       listDto.setOperationTypeList(this.operationTypeService.list(wrapper));
        return listDto;
    }
    //添加
    @RequestMapping("addRecord")
    public MsgUtil addPati(@RequestBody OperationRecord operationRecord) {
        String str="16153";
        Random rand = new Random();
        int a=rand.nextInt(89999999)+10000000;
        operationRecord.setHorCard(str+a);
        OperationType operationType=this.operationTypeService.getById(operationRecord.getHoyId());
        BigDecimal p1= operationType.getHoyPrice();
        BigDecimal p2= operationType.getHoyPriceWorker();
        operationRecord.setHorPrice(p1.add(p2));
        operationRecord.setHorCreateTime(new Date());
        operationRecord.setEqIsdel(0);
        System.out.println(operationRecord);
        Boolean bool=this.operationRecordService.save(operationRecord);
        // Integer bool=this.doctorAdviceService.addDoct(doctorAdvice,list);
        if (bool){
            return new MsgUtil(true,"添加成功",null);
        }else {
            return new MsgUtil(false,"添加失败",null);
        }
    }
    //修改
    @RequestMapping("upRecord")
    public MsgUtil upRecord(@RequestBody OperationRecord operationRecord) {
        Boolean bool=this.operationRecordService.updateById(operationRecord);
        // Integer bool=this.doctorAdviceService.addDoct(doctorAdvice,list);
        if (bool){
            return new MsgUtil(true,"修改成功",null);
        }else {
            return new MsgUtil(false,"修改失败",null);
        }
    }
    //显示修改ui
    @RequestMapping("upRecordui")
    public OperationRecord upPatiui(Integer id) {
        OperationRecord hoPay=this.operationRecordService.getById(id);
        return hoPay;
    }
}
